package com.freshfresh.activity.selfcenter;


import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.R;
import com.freshfresh.utils.FreshConstants;
import com.freshfresh.utils.ImageLoadOptions;
import com.freshfresh.utils.RequestManager;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
import com.freshfresh.view.AlertDialog;
import com.nostra13.universalimageloader.core.ImageLoader;

//我的两鲜

public class SelfCenterActivity extends Activity{

	private ImageView iv_head_photo=null;
	Dialog progressDialog;
	private TextView tv_ac_user_center_blance=null;//账户余额
	private TextView tv_ac_user_center_coupon=null;//优惠券
	private TextView tv_ac_user_center_score=null;//积分
	private TextView tv_ac_user_center_nickname=null;//昵称
	
	
	private ImageView iv_ac_user_center_calling2=null;//电话
	private LinearLayout ll_personal_information=null;//我的资料
	private LinearLayout ll_address_manager=null;//地址管理
	private LinearLayout lljifen=null;//我的积分
	private LinearLayout line_account=null;//我的账户
	private LinearLayout line_coupon=null;//优惠券
	private LinearLayout ll_favourite=null;//我的收藏
	
	private ImageView iv_setup=null;
	String headurl;
	private BroadcastReceiver br=new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			if(!FreshConstants.hasLogined(SelfCenterActivity.this)){
				iv_head_photo.setImageResource(R.drawable.app_icon);
				tv_ac_user_center_blance.setText("0.00");
				tv_ac_user_center_score.setText("0");
				tv_ac_user_center_coupon.setText("0张");
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ac_user_center);
		registerReceiver(br, new IntentFilter(FreshConstants.LOGOUT));
		initView();
		if(!FreshConstants.hasLogined(SelfCenterActivity.this)){
		  	
		}else{
		  getUserInfo();
		}
		
	}
	public void initView(){
		progressDialog = Utils.createLoadingDialog(SelfCenterActivity.this, "正在加载……");
		
		iv_setup=(ImageView)findViewById(R.id.iv_setup);
		line_coupon=(LinearLayout)findViewById(R.id.line_coupon);
		lljifen=(LinearLayout)findViewById(R.id.lljifen);
		ll_address_manager=(LinearLayout)findViewById(R.id.ll_address_manager);
		line_account=(LinearLayout)findViewById(R.id.line_account);
		iv_ac_user_center_calling2=(ImageView)findViewById(R.id.iv_ac_user_center_calling2);
		tv_ac_user_center_nickname=(TextView)findViewById(R.id.tv_ac_user_center_nickname);
		ll_personal_information=(LinearLayout)findViewById(R.id.ll_personal_information);
		ll_favourite=(LinearLayout)findViewById(R.id.ll_favourite);
		tv_ac_user_center_score=(TextView)findViewById(R.id.tv_ac_user_center_score);
		tv_ac_user_center_blance=(TextView)findViewById(R.id.tv_ac_user_center_blance);
		tv_ac_user_center_coupon=(TextView)findViewById(R.id.tv_ac_user_center_coupon);
		iv_head_photo=(ImageView)findViewById(R.id.iv_head_photo);
		iv_head_photo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(!FreshConstants.hasLogined(SelfCenterActivity.this)){
					Intent intent=new Intent(SelfCenterActivity.this,LoginActivity.class);
					startActivityForResult(intent, 250);	
				}else{
					Toast.makeText(SelfCenterActivity.this, "功能待开发", 0).show();
				}
				
			}
		});
		
		ll_favourite.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(!FreshConstants.hasLogined(SelfCenterActivity.this)){
					Intent intent=new Intent(SelfCenterActivity.this,LoginActivity.class);
					startActivityForResult(intent, 251);	
				}else{
					Intent intent=new Intent(SelfCenterActivity.this,MyFavoriteActivity.class);
					startActivity(intent);	
				}
				
			}
		});
		
		line_coupon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(!FreshConstants.hasLogined(SelfCenterActivity.this)){
					Intent intent=new Intent(SelfCenterActivity.this,LoginActivity.class);
					startActivityForResult(intent, 251);	
				}else{
					Toast.makeText(SelfCenterActivity.this, "功能待开发", 0).show();
				}
				
			}
		});
		
		
		iv_setup.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
					Intent intent=new Intent(SelfCenterActivity.this,MoreInfoActivity.class);
					startActivity(intent);	
				
			}
		});
		line_account.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(!FreshConstants.hasLogined(SelfCenterActivity.this)){
					Intent intent=new Intent(SelfCenterActivity.this,LoginActivity.class);
					startActivityForResult(intent, 252);	
				}else{
					Toast.makeText(SelfCenterActivity.this, "功能待开发", 0).show();
				}
				
			}
		});
		
		ll_personal_information.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent(SelfCenterActivity.this,UserInfoActivity.class);
				SharedPreferences userShared = FreshConstants
						.getUserShared(SelfCenterActivity.this);
				intent.putExtra("headurl", userShared.getString("head_url", ""));
				startActivityForResult(intent,123);
			}
		});
		
		iv_ac_user_center_calling2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				new AlertDialog(SelfCenterActivity.this).builder().setMsg("是否拨打两鲜热线:" + getResources().getString(R.string.liangxian_tel) + "?").setNegativeButton("", null).setPositiveButton("拨打", new View.OnClickListener()
		        {
		          public void onClick(View paramAnonymousView)
		          {
		        	  Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
								+ getResources().getString(R.string.liangxian_tel)));
						startActivity(intent);
		          }
		        }).show();
			}
		});
		
		ll_address_manager.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(SelfCenterActivity.this,AddressListActivity.class);
				startActivity(intent);
			}
		});
		
		
		lljifen.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(!FreshConstants.hasLogined(SelfCenterActivity.this)){
					Intent intent=new Intent(SelfCenterActivity.this,LoginActivity.class);
					startActivityForResult(intent, 253);
				}else{
					Intent intent=new Intent(SelfCenterActivity.this,MemberPointsActivity.class);
					startActivity(intent);
				}
			}
		});
	}
			
			//获取个人信息
			private void getUserInfo(){
				
				progressDialog.show();
				SharedPreferences userShared = FreshConstants.getUserShared(SelfCenterActivity.this);
				String customerid=userShared.getString("userid", "");
				String token=userShared.getString("token", "");
				Map<String, String> params = new HashMap<String, String>();
				params.put("customerid", customerid);
				params.put("token", token);
				executeRequest(new StringRequest(UrlConstants.getUserInfo, params,
						new Response.Listener<String>() {

							@Override
							public void onResponse(String arg0) {
								progressDialog.dismiss();
								Log.e("arg0个人信息是多少………………", arg0);
								if (Utils.parseJsonStr(arg0).get("result").toString().equals("1")) {
									Map<String,Object> pointmap=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
									if(pointmap.get("balance")!=null){
										tv_ac_user_center_blance.setText(pointmap.get("balance").toString());
									}
									if(pointmap.get("current_point")!=null){
										tv_ac_user_center_score.setText(pointmap.get("current_point").toString());
									}
									if(pointmap.get("coupon_count")!=null){
										tv_ac_user_center_coupon.setText(pointmap.get("coupon_count").toString()+"张");
									}
									//昵称
									if(pointmap.get("nikename")!=null){
										tv_ac_user_center_nickname.setText(pointmap.get("nikename").toString());
									}
									//头像
									if(pointmap.get("head_url")!=null){
										headurl=pointmap.get("head_url").toString();
										SharedPreferences userShared = FreshConstants
												.getUserShared(SelfCenterActivity.this);
										Editor editor = userShared.edit();
										editor.putString("head_url", headurl);
										editor.commit();
										ImageLoader.getInstance().displayImage(pointmap.get("head_url").toString(), iv_head_photo, ImageLoadOptions.getOptions(R.drawable.app_icon));
									}
									
								} else {
									
								}
							}

						}, new Response.ErrorListener() {

							@Override
							public void onErrorResponse(VolleyError arg0) {
								progressDialog.dismiss();
							}
						}));
			  }
	
	protected void executeRequest(Request request) {
		RequestManager.addRequest(request, this);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case 205:
			ImageLoader.getInstance().displayImage(data.getStringExtra("url"), iv_head_photo, ImageLoadOptions.getOptions(R.drawable.app_icon));
			break;
		case 201:
			getUserInfo();
			break;
		default:
			break;
		}
	}
}
